<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_yiliao','merchid'=>$merchid),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$fenleiid=intval($_RQ['fenlei']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$tip='如果场景前端支持展示文章的时候，前端才会文章出来！！';
	
	$params['plugin_name']='jy_yiliao';
	$tip .='</br><red>【注意】：本场景已开启独立展示，只显示本场景下的文章！</red>';
	
	$fields=array('id','title','fenlei','pic_url','keyword','createtime','readnum','likenum','status','content','paixu','workerid');
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';

	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['content'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['createtime']=date('y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['edit']=array('op'=>'edit','action'=>'account.article','query'=>'id='.$item['id']);
			$item['url']=custom_url('jy_merch/news.list/detail/id='.$item['id'],true);
			$member=pdo_get('jy_yiliao_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
			if($member){
			    $item['wmember']=$member['name'].'('.$member['mobile'].')';
			}else{
			    $item['wmember']='无';
			}
			//$item['wkurl']=custom_url('jy_yiliao/worker_news/detail/id='.$item['id'],true);
			
		}
	}
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['list']=$list?$list:array();
	$return['fenlei']=$fenlei?$fenlei:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'文章列表'),
			array('op'=>'edit','title'=>'添加文章'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>false,'list'=>array(
		    array('name'=>'paixu','title'=>'排序(点击编辑↓)','type'=>'string','set'=>'setsort'),
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'fenlei','title'=>'文章分类','type'=>'string'),
			array('name'=>'keyword','title'=>'关键字','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			array('name'=>'likenum','title'=>'点赞量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('type'=>'string','title'=>'所属医生','name'=>'wmember'),
			//array('name'=>'wkurl','title'=>'医生文章二维码','type'=>'ewm'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('op'=>'edit','title'=>'编辑','type'=>'href'),
			array('title'=>'选择医生','op'=>'bindworker','type'=>'href'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='bindworker'){
    if($_RQ['postdata']){
        $post=get_postdata();
    	$id=$post['id'];
    	if(empty($post['worker_id'])){
    		exi('请选择医生！','error');
    	}
    	$worker_new = pdo_get('jy_yiliao_worker_news',array('workerid'=>$post['worker_id'],'fromid'=>$id,'uniacid'=>$_SESSION['uniacid']));
    
    	if(!$worker_new && $post['worker_id']){
		    $worker = pdo_get('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid'],'id'=>$post['worker_id']));
			if($post['worker_id']){
    			$news=pdo_get('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
    			$data['title']=$news['title'];
    			$data['desc']=$news['desc'];
    			$data['image']=$news['pic_url'];
    			$data['content']=$news['content'];
    			$data['fromid'] = $id;
    		}
    		$data['workerid']=$post['worker_id'];
    		$data['uniacid']=$_SESSION['uniacid'];
    		$data['createtime']=TIMESTAMP;
    		pdo_insert('jy_yiliao_worker_news',$data);
		    pdo_update('core_account_article',array('workerid'=>$post['worker_id']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}
    	exi('绑定成功！','','//list/');
    }
    $id=intval($_RQ['id']);
  
	if(!$id){
		exi('请选择文章','error');
	}
	$return['item']=pdo_get('core_account_article',array('id'=>intval($_RQ['id'])));
	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'绑定医生',
	);
	$htmls['edit']=array(
		array('type'=>'choose','name'=>'worker_id','title'=>'绑定的医生','choose_title'=>'选择医生','help'=>'绑定后，文章归属于该医生','action'=>'jy_yiliao/merch.get_worker//'),
	);
	$return['htmls']=$htmls;
	exi($return);
    
}
if($_RQ['op']=='setsort'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择文章！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['paixu'])){
		$update['paixu']=$post['paixu'];
	}
	if($update){
		pdo_update('core_account_article',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('core_account_article',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_yiliao','merchid'=>$merchid);
	$return['fenlei']=pdo_getall('core_account_article_fenlei',$params,array('id','title'));
	$id = intval($_RQ['id']);
	if ($id) {
		$return['item']=pdo_get('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//$return['item']['content']=htmlspecialchars_decode($return['item']['content']);
		$return['item']['advs']=json_decode($return['item']['advs'],true);
		//unset($return['item']['status'],$return['item']['adtype'],$return['item']['re_readnum'],$return['item']['re_likenum'],$return['item']['article_rule_daynum'],$return['item']['article_rule_allnum'],$return['item']['article_rule_credit'],$return['item']['article_rule_money'],$return['item']['article_rule_money_total'],$return['item']['article_rule_userd_money'],$return['item']['yingxiao']);
	}else{
		$return['item']=array('advs'=>array());
	}
	if($_SESSION['os']!='windows'){
		$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'文章信息',
			'tabs'=>array(
				array('op'=>'list','title'=>'文章列表'),
				array('op'=>'edit','title'=>'编辑列表'),
			),
			'edit'=>array(
				'0'=>array('type'=>'string','title'=>'文章标题','name'=>'title', 'help'=>'' ,'must'=>true),
				'1'=>array('type'=>'string','title'=>'文章排序','name'=>'paixu', 'help'=>'填写100以内正整数，数值越小越靠前' ),
				'2'=>array('type'=>'switch','title'=>'是否显示','name'=>'status','help'=>'' ),
				'3'=>array('type'=>'select','title'=>'文章分类','name'=>'fenlei','options'=>'fenlei' ),
				'4'=>array('type'=>'richtextold','title'=>'文章内容','name'=>'content' , 'help'=>'','must'=>true),
			)
		);
		$return['htmls']=$htmls;
		exi($return);
	}
	$return['step']=$return['sucai']=1;
	$return['date']=date('Y-m-d');
	$return['account_title']=$_SESSION['title'];
	$return['bans']=array(
		'1'=>'禁止复制链接',
		'2'=>'禁止分享至朋友圈',
		'3'=>'禁止分享给好友'
	);
	$return['yingxiaos']=array(
		'0'=>'关闭此功能',
		'1'=>'只显示第一张',
		'2'=>'随机显示',
		'3'=>'轮播显示'
	);
	$return['shows']=array(
		'1'=>'微信端',
		'2'=>'手机端'
	);
	$return['account']=array(
		'1'=>'显示',
		'0'=>'不显示'
	);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	
	$fields=array('title','fenlei','keyword','readnum','likenum','status','author','adtype','adtitle','admore','date','account','oldurl','article_rule_daynum','article_rule_allnum','article_rule_credit','article_rule_money','article_rule_money_total','article_rule_userd_money','ban','shows','desc','pic_url','content','yingxiao','adurl','advs','merchid');
	$data=array_fields($fields,$postdata);
	$data['advs']=ijson_encode($data['advs']);
	if(empty($data['title'])){
		exi('请填写文章标题！','error');
	}
	if(empty($data['fenlei'])){
		exi('请填写文章分类！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_article',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['plugin_name']='jy_yiliao';
		$data['createtime']=TIMESTAMP;
		$data['merchid'] = $merchid;
		pdo_insert('core_account_article',$data);
		$id=pdo_insertid();
	}
	if($data['keyword']){
		$parameter=array();
		$parameter['action']='article.detail';
		$parameter['id']=$id;
		$parameter['keyword']=$data['keyword'];
		$parameter['title']=$data['title'];
		$parameter['description']=$data['desc'];
		$parameter['picurl']=$data['pic_url'];
		$parameter['status']=$data['status'];
		$parameter['url']=custom_url('core/article.list/detail/id='.$id);
		cfc('core')->save_back_news($parameter);
	}
	exi('编辑文章成功！','','list');
}

